<?php
	require_once('dbfunctions.php');
	$stud = new Student($_SESSION['user'],$_SESSION['psw'],$_SESSION['idpsw']);

	if(isset($_GET['vote'])){
		$v = explode('-',$_GET['vote']);
		if(!( isset($v[1]) && $stud->vote($v[0],$v[1]) ))
				echo '<script type="text/javascript">alert("Errore. Voto non convalidato. Riprovare");</script>';
	}
?> 
	
	<div id="block">
    	
    	<p> 
				Benvenuto\a  
<?php 				
					echo $stud->getName(),' ',$stud->getSurname(); 
					
?> 
				:) <br>
				<a id="logout" href="?out=0">Logout</a>
		</p>
		
		<div>
				Seleziona corso:
				<form method="get">
					<select name="cid">
<?php
					$corsi = $stud->getClasses();
					foreach($corsi as $corso) 
							echo '<option value="',$corso[0],'">',$corso[1],'</option>';					
?>
					</select>
					<input type="submit" value="Visualizza">
				</form>
			
<?php
if(isset($_GET['cid']) && $stud->attends($_GET['cid'])){
		
		if($stud->hasQuestions($_GET['cid'])){
?>
				<script language="javascript"> 
					window.open ('question.php?cid=<?php echo $_GET['cid'];?>, 'newwindow');
				</script>
<?php 				
		echo '<h2><a href="question.php?cid=',$_GET['cid'],'" target="question">Rispondi al questionario!</a></h2>';

		}
		
		$media = $stud->drawGraph($_GET['cid']);
		printf('<div>Valutazione media totale: <b><i>%.1f</i></b>/30<br>Frequenza media: <b><i>%d</i></b> </div>', $media[0],$media[1]);
		
		/*if(isset($_POST['com']))
			$stud->addComment($_GET['cid'],$_POST['com']);
			
		$coms = $stud->getComments($_GET['cid']);

		if(!isset($_GET['com'])) 
			echo '<a href="?cid=',$_GET['cid'],'&com=1">Visualizza commenti</a>';
		
		else{
?>	
	<table id="comments">
<?php 

			$i=0;
			foreach($coms as $com){
				if($i++ % 2) 
					echo '<tr><td bgcolor="676FFF"><b>';
				else 
					echo '<tr><td bgcolor="#99CDFF"><b>';
				
					if(is_null($com[0])) 
					echo 'Professore:';
				else 
					echo $com[0],' ',$com[1];;
				echo '</b><br>',date('d/m H:i',strtotime($com[3])),'<br>', $com[2],'</td></tr>';
			}
?>
			<tr><td bgcolor="#CCFFCC"><form method="post" action="?cid=<?php echo $_GET['cid'];?>">
								<textarea rows="2" cols="30" name="com">Commenta...</textarea>
								<input type="submit" value="Invia">
							</form></td></tr>
			</table>
<?php	
			echo '<a href="?cid=',$_GET['cid'],'">Nascondi commenti</a>';
		}*/
		
?>
			<table id="marktable">
 				<tr id="firstrow"><td>Data</td><td>Info</td><td>Materiale</td><td class="vote">Vota!</td></tr>
<?php
		$lessons = $stud->getLessons($_GET['cid']);
			foreach($lessons as $lesson){
			 		echo '<tr><td>',date('d/m',$lesson[4]),'</td><td>',$lesson[2],'</td><td>';
			 		
			 		if(!is_null($lesson[3]))
			 			echo '<a href="',$lesson[3],'" target="dl">Download dispense</a>';
			 		echo '</td><td>';
			 		
			 		if(is_null($lesson[1]) && $stud->noResetAfter($lesson[4])){
			 			echo '<form method="get"><input type="hidden" value="',$_GET['cid'],'" name="cid" /><select name="vote">';
			 			for($i=30;$i>-1;--$i)
			 				echo '<option value="',$lesson[0],'-',$i,'">',$i,'</option>';
			 			echo '</select><input type="submit" value="Conferma"></form>';
			 		}
			 		echo '</td></tr>';
			 }
		}
		?></table><?php
			 


?> 		
			
		</div>
	</div>